﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AzureGames.Web.Models;
using Microsoft.Practices.ServiceLocation;
using AzureGames.Domain.Models;

using Handlers = AzureGames.Domain.Handlers;

namespace AzureGames.Web.Controllers
{
    public class GameController : AuthorizedController
    {
        public GameController(IServiceLocator serviceLocator) : base(serviceLocator) { }

        public ActionResult List()
        {
            //var getClaim = this.GetClaimValue("Name");

            var games = Using<Handlers.GetGameDefinitionList>().Execute();

            var model = new DashboardViewModel
            {
                GameListViewModel = new GameDefinitionListViewModel(games)
            };

            return View(model);
        }

        [Authorize]
        public ActionResult Play(Guid boardID)
        {
            // after notification, the gameboard is retrieved and Play is called
            var gameBoard = Using<Handlers.GetGameBoard>().Execute(boardID);
            return View(gameBoard);
        }

        public ActionResult NonMemberPlay(int gameId, string opponentId)
        {            

            var gameBoard = Using<Handlers.CreateNewGame>().Execute(gameId, new List<string> { Guid.Empty.ToString(), opponentId });      
            return View("Play", gameBoard);
        }

        [OutputCache(Duration = int.MaxValue, VaryByParam = "resourceID")]
        public FileContentResult GetResource(Guid resourceID, string contentType)
        {
            return new FileContentResult(Using<Handlers.GetResource>().Execute(resourceID).Bytes, contentType);
        }

        [HttpPost]
        public JsonResult JsonDetails(int id)
        {
            //            Guid userId = User.Identity.IsAuthenticated ? ((AzureGamesIdentity)User.Identity).UserId : Guid.Empty;                        
            Model.User user = Session["User"] as Model.User;
            string playerId = user == null ? Guid.Empty.ToString() : user.ID;

            var gameDef = Using<Handlers.GetGameDetails>().Execute(id, playerId);

            return Json(gameDef);
        }

        [HttpPost]
        public JsonResult JsonExtend(int id)
        {
            //            Guid userId = User.Identity.IsAuthenticated ? ((AzureGamesIdentity)User.Identity).UserId : Guid.Empty;
            Model.User user = Session["User"] as Model.User;
            string playerId = user == null ? Guid.Empty.ToString() : user.ID;

            var gameDef = Using<Handlers.GetGameExtend>().Execute(id, playerId);

            return Json(gameDef);
        }

        [HttpPost]
        public JsonResult JsonPlayOptions(int id)
        {
            //            Guid userId = User.Identity.IsAuthenticated ? ((AzureGamesIdentity)User.Identity).UserId : Guid.Empty;
            Model.User user = Session["User"] as Model.User;
            string playerId = user == null ? Guid.Empty.ToString() : user.ID;

            var gameDef = Using<Handlers.GetGamePlayOptions>().Execute(id, playerId, 10);

            return Json(gameDef);
        }

        [HttpPost]
        [Authorize]
        public JsonResult JsonRegisterGame(int gameId)
        {
            Model.User user = Session["User"] as Model.User;

            var boardID = Using<Handlers.RegisterNewGame>().Execute(gameId, user.ID);

            // the user will then wait on the Configure screen until notified the game is ready
            return Json(boardID);
        }

        [HttpPost]
        [Authorize]
        public JsonResult JsonCreateGame(int gameId, string opponentPlayerId)
        {
            Model.User user = Session["User"] as Model.User;

            var boardID = Using<Handlers.CreateNewGame>().Execute(gameId, new List<string> { user.ID, opponentPlayerId });

            // the user will then wait on the Configure screen until notified the game is ready
            return Json(boardID);
        }

        [Authorize]
        [HttpPost]
        public JsonResult JsonGetBoard(Guid boardId)
        {
            return Json(Using<Handlers.GetBoard>().Execute(boardId));
        }

        [Authorize]
        [HttpPost]
        public JsonResult JsonUpdateBoard(Model.Board board)
        {            
            return Json(Using<Handlers.UpdateBoard>().Execute(board));         
        }

        [Authorize]
        [HttpPost]
        public JsonResult JsonGetGameUser(string userId)
        {            
            return Json(Using<Handlers.GetGameUser>().Execute(userId));
        }



    }
}
